package com.uni.base;

import com.alibaba.fastjson.JSON;

/*
 * @Description: 自定义响应数据结构
 */
public class RestResponse<T> {
    private int code;
    private String message;
    private T response;

    public RestResponse(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public RestResponse(int code, String message, T response) {
        this.code = code;
        this.message = message;
        this.response = response;
    }

    public static RestResponse fail(Integer code, String msg) {
        return new RestResponse<>(code, msg);
    }
    public static RestResponse fail(String msg) {
        return new RestResponse<>(-1, msg);
    }

    public static RestResponse ok() {
        SystemCodeEnum systemCodeEnum = SystemCodeEnum.OK;
        return new RestResponse<>(systemCodeEnum.getCode(), systemCodeEnum.getMessage());
    }
    public static RestResponse ok(String message) {
        SystemCodeEnum systemCodeEnum = SystemCodeEnum.OK;
        return new RestResponse<>(systemCodeEnum.getCode(), message);
    }

    /**
     * @param <F>      the type parameter
     * @param response the response
     * @return the rest response
     */
    public static <F> RestResponse<F> ok(F response) {
        SystemCodeEnum systemCodeEnum = SystemCodeEnum.OK;
        return new RestResponse<>(systemCodeEnum.getCode(), systemCodeEnum.getMessage(), response);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }


    public void setMessage(String message) {
        this.message = message;
    }

    public T getResponse() {
        return response;
    }

    public void setResponse(T response) {
        this.response = response;
    }

    @Override
    public String toString() {
        return "RestResponse{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", response=" + response +
                '}';
    }

    public String toJSONString(){
        return JSON.toJSONString(this);
    }
}